package com.turtle.controller;

import com.turtle.dto.BlogSortInfoDto;
import com.turtle.dto.BlogSortListDto;
import com.turtle.dto.BlogSortParam;
import com.turtle.dto.BlogSortStatusParam;
import com.turtle.service.BlogSortService;
import com.turtle.valid.group.Update;
import com.turtle.vo.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author lijiayu
 * @date 2020/7/21
 * @description 博客分类相关接口
 */
@RestController
@RequestMapping("/blog-sort")
public class BlogSortController {

    @Autowired
    private BlogSortService blogSortService;

    @GetMapping("/list")
    public ResultBody list(@RequestParam("status") Integer status,@RequestAttribute Long userId){
        List<BlogSortListDto> list = blogSortService.getList(status,userId);
        return ResultBody.result(list);
    }

    @GetMapping
    public ResultBody get(@RequestParam("id") Long id){
        BlogSortInfoDto blogSortInfoDto = blogSortService.get(id);
        return ResultBody.result(blogSortInfoDto);
    }

    @PostMapping
    public ResultBody add(@RequestBody @Validated BlogSortParam param,@RequestAttribute Long userId){
        blogSortService.add(param,userId);
        return ResultBody.success();
    }

    @PutMapping
    public ResultBody update(@RequestBody @Validated({Update.class}) BlogSortParam param,
                             @RequestAttribute Long userId){
        blogSortService.update(param,userId);
        return ResultBody.success();
    }

    @DeleteMapping
    public ResultBody delete(@RequestParam("id")Long id,@RequestAttribute Long userId){
        blogSortService.delete(id,userId);
        return ResultBody.success();
    }

    @PutMapping("/status")
    public ResultBody updateStatus(@RequestBody @Valid BlogSortStatusParam param,
                                   @RequestAttribute Long userId){
        blogSortService.updateStatus(param,userId);
        return ResultBody.success();
    }
}
